package com.yms.socket.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

//*
// * @version : 1.0
// * @author: yangmingsong
// * @date: 2022/7/1-07-01-14:49
// * @description: spring5Test 客户端------>>>>>---服务端


public class BIOServer {


    public static void main(String[] args) throws IOException {
        productServer();
    }

    private static void productServer(){
        try {
            Scanner scanner = new Scanner(System.in);
            ServerSocket serverSocket = new ServerSocket(1023);
            //2、服务端通过accept方法用于监听客户端Socket请求
            Socket socket = serverSocket.accept();
            //3、从Socket中获取字节输入或输出流进行数据得读写传输
            InputStream inputStream = socket.getInputStream();
            //4、将字节输入流包装成一个缓冲字符输入流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            OutputStream outputStream = socket.getOutputStream();
            PrintStream printStream = new PrintStream(outputStream);
            new Thread(){
                @Override
                public void run() {
                    while (true){
                        String str = scanner.next();
                        printStream.println(str);
                        printStream.flush();

                    }
                }
            }.start();
            new Thread(){
                @Override
                public void run() {
                    String s=null;
                    while (true){
                        try {
                            if (!((s = bufferedReader.readLine()) != null)) {break;};
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.println("客户端1发来得消息"+s);
                    }
                }
            }.start();
        }catch (Exception e){
            System.out.println("404了--------");
        }
    }









}
